<?php

namespace app\controller\api;

use app\controller\ApiBaseController;
use app\model\OrganizationModel;

class Org extends ApiBaseController
{

    /**
     * 添加组织
     *
     * @return void
     */
    public function addOrg()
    {
        try {
            //接收参数
            $strName = param_filter($this->request->param('name', ''), 'string');
            if (empty($strName)) {
                return api_response([], '缺少组织名称', 0);
            }
            $arrData = ['name' => $strName];//组织名称必传
            $strContactName = param_filter($this->request->param('contact_name', ''), 'string');
            if (!empty($strContactName)) {
                $strContactName['contact_name'] = $strContactName;
            }
            $strContactPhone = param_filter($this->request->param('contact_phone', ''), 'string');
            if (!empty($strContactPhone)) {
                $strContactName['contact_phone'] = $strContactPhone;
            }

            $boolAddOrg = (new OrganizationModel())->addOrg($arrData);
            if (!$boolAddOrg) {
                return api_response([], '添加组织失败', 0);
            }
            return api_response();
        } catch (\Exception $e) {
            return api_response([], '系统异常：' . $e->getMessage(), 0);
        }
    }

    /**
     * 更新组织信息
     *
     * @return void
     */
    public function upOrgInfo()
    {
        try {
            //接收参数
            $intId = param_filter($this->request->param('id', 0), 'int');
            if (empty($intId)) {
                return api_response([], '请选择一个组织', 0);
            }

            $arrUpData = [];
            $strName = param_filter($this->request->param('name', ''), 'string');
            if (!empty($strName)) {
                $arrUpData['name'] = $strName;
            }
            $strContactName = param_filter($this->request->param('contact_name', ''), 'string');
            if (!empty($strContactName)) {
                $arrUpData['contact_name'] = $strContactName;
            }
            $strContactPhone = param_filter($this->request->param('contact_phone', ''), 'string');
            if (!empty($strContactPhone)) {
                $arrUpData['contact_phone'] = $strContactPhone;
            }
            $intStatus = param_filter($this->request->param('status', 0), 'int');
            if (!empty($intStatus)) {
                $arrUpData['status'] = $intStatus;
            }

            if (empty($arrUpData)) {
                return api_response([], '没有需要更新的信息', 0);
            }

            OrganizationModel::updateOneByWhere(
                [['id', '=', $intId]],
                $arrUpData
            );
            return api_response();
        } catch (\Exception $e) {
            return api_response([], '系统异常：' . $e->getMessage(), 0);
        }
    }

    /**
     * 获取组织列表
     *
     * @return void
     */
    public function getOrgList()
    {
        try {
            $arrWhere = [];
            //接收参数
            $strName = param_filter($this->request->param('name', ''), 'string');
            if (!empty($strName)) {
                $arrWhere[] = ['name', 'like', '%' . $strName . '%'];
            }
            $intStatus = param_filter($this->request->param('status', 0), 'int');
            $arrWhere[] = ['status', '=', $intStatus];

            $intTotal = OrganizationModel::getAllCount($arrWhere);
            if (empty($intTotal)) {
                return api_response(
                    [
                        'total' => 0,
                        'list' => [],
                    ],
                    '',
                    1
                );
            }
            $arrList = OrganizationModel::getPageList(
                $this->pageIndex,
                $this->pageSize,
                $arrWhere,
                ['*'],
                'id',
                'asc'
            );
            foreach ($arrList as $key => $value) {
                $arrList[$key]['status'] = OrganizationModel::getStatusText($value['status']);
            }
            return api_response(
                [
                    'total' => $intTotal,
                    'list' => $arrList,
                ]
            );
        } catch (\Exception $e) {
            return api_response([], '系统异常：' . $e->getMessage(), 0);
        }
    }
}
